﻿#ifndef _REC_READ_ASF_H_
#define _REC_READ_ASF_H_

#include "rec_read_base.h"
#include "asf_info.h"

typedef union _ASF_OBJ_INCLUDED_
{
    int32_t	value;
	struct
	{
		int32_t have_file_propert     : 1;
		int32_t have_contern_obj      : 1;
		int32_t have_video_stream_obj : 1;
		int32_t have_audio_stream_obj : 1;
		int32_t have_codec_lst_obj    : 1;
		int32_t have_externsion_obj   : 1;
	};
}ASF_OBJ_INCLUDED;

class CRecReadASF:public CRecReadBase
{
public:
	CRecReadASF(int clientID = 0,int channel = -1);
	~CRecReadASF();
	virtual	int Open(const char * dev, uint32_t fileIndex,int64_t startTime = 0, int64_t endTime = 0,bool bClearBuf = false);
   	virtual int ReadFIFONormal();
	virtual int ReadFIFO2Forward();
	virtual int ReadFIFOForward() ;	
	virtual int ReadFIFOReback() ;	

	virtual int ReadFrame(uchar_t* frameBuf, uint32_t bufLen);
	int CreateFrame(uchar_t * dataBuf, uint32_t bufSize);
	int ReadASFHead();
	int read_data_object_to_largebuf(); 
	uint32_t AlignToDataPacket(uint32_t srcOffset);
	
protected:
	//在拖动进度条后需要定位到关键帧来解析
	int AnalyzeAsfToFIFO(uchar_t * frameBuf, uint32_t bufLen,bool bSearchKey = false);
	uint32_t CreateFrmData(uchar_t * frameBuf, int bufLen);
	int CreateFrameToFIFO(struct single_payload_t & payload,uchar_t * frmBuf = NULL,uint32_t frmSize = 0);

	//快进,快退中使用
	uint32_t GetReadPacketSize(uint32_t offset,uint32_t frmSize);
	int  AnalyzeKeyFrame(uchar_t * frameBuf, uint32_t bufLen,uint32_t keyFrmSize);

	void get_asfvideotime_from_fileprop(uint64_t fplaytime, struct asfvideo_time_t *asftime);
	int  show_file_properties_obj(struct asf_file_properties_object_t *tmpfilepropobj);
	void get_file_create_timestr(int32_t filetimet, char *timeBuf, int timeBufSz);
	int file_time_to_unix_time(uint64_t fftime);
	int read_content_description_object(struct asf_content_description_object_t *pContentdescriptionobj,bool debugflag = false);
	int read_video_stream_properties_object(struct asf_video_stream_properties_object_t *tmpvideostreampropobj, int debugflag);
	int read_audio_stream_properties_object(struct asf_audio_stream_properties_object_t *tmpaudiostreampropobj, int debugflag);
	int read_codec_list_object( struct asf_codec_list_onject_t *tmpcodeclistobj, int debugflag);
	int read_header_extension_object( struct asf_header_extension_object_t *tmpheaderextobj, int debugflag);
protected:
	struct asf_header_object_t			 m_header_obj;
	struct asf_file_properties_object_t  m_fileprop_object;
	struct asf_header_extension_object_t m_header_extension_obj;
	struct asf_language_list_object_t    m_lang_list_obj;
	struct asf_simple_index_object_t	 m_simple_index_obj;
	struct asf_padding_object_t          m_padding_obj;
	struct asf_content_description_object_t m_content_description_obj;
	struct asf_video_stream_properties_object_t m_video_stream_properties_obj;
	struct asf_audio_stream_properties_object_t m_audio_stream_properties_obj;
	struct asf_codec_list_onject_t		 m_codec_list_obj;
	struct asf_data_object_t			 m_data_obj;
	struct curr_datapacket_status_t		 m_curr_datapacket_status;
	struct write_datapacket_parameter_t  m_g726_write_datapacket_para;
	struct write_datapacket_parameter_t  m_write_datapacket_para;
	
	uchar_t* m_pkt_buf;       //数据包缓冲区
	uint32_t m_pkt_off;		  //数据包的偏移
	uint32_t m_data_pkt_size; //数据包大小 
	uint32_t m_frameNO;		  //生成的帧编号
	ASF_OBJ_INCLUDED m_obj_inc;
	FRAME_CRT_ADDR m_frmAddr;   //当前的帧的缓冲地址
};


#endif //_REC_READ_H_
